package cn.net.wanji.job;

import cn.net.wanji.service.MonitorService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RefreshIndexScheduledTask {

    @Autowired
    private MonitorService monitorService;

    public static JSONObject jsonObject = null;
    private ScheduledExecutorService scheduler;

    // 在初始化时启动定时任务
    @PostConstruct
    public void init() {
        scheduler = Executors.newSingleThreadScheduledExecutor();
        scheduler.scheduleAtFixedRate(this::myScheduledTask, 0, 3, TimeUnit.SECONDS);
        log.info("定时任务初始化完成");
    }

    // 定时任务
    private void myScheduledTask() {
        long startTime = System.currentTimeMillis();
        // 获取当前日期
        LocalDateTime currentDate = LocalDateTime.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化当前日期
        String date = currentDate.format(formatter);
        jsonObject = monitorService.monitorData(date);
        long endTime = System.currentTimeMillis();
        log.info("data首页接口数据处理时间====》{}", endTime - startTime);
    }


}
